package com.wbao.dianniu.ui;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.QAInfoAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.MySendQaManager;
import com.wbao.dianniu.manager.QaDataChangeManager;
import com.wbao.dianniu.manager.QaDataChangeManager.IQaDataListener;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 我的收藏
 * 
 * @author yejianjun
 * 
 */
public class MyCollectionActivity extends BaseActivity implements IQaListener, OnClickListener,IQaDataListener {

	private QAInfoAdapter adapter;
	private PullToRefreshListView pullListView;
	private ListView mListView;
	private MySendQaManager manager;
	private List<QaResponse> mList;
	private int TYPE = 5;
	private int currentPage = 0;
	private final int PAGE_COUNTS = 10;
	private TextView noDataTV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_my_collection,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.my_collection));

		initView();
		requestData();
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// 点击问题 跳转到问题详情
				intentDetails(mList.get(position-1));
			}
		});
		QaDataChangeManager.getInstance().addQaChangeListener(this);
	}

	private void initView() {
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		
		pullListView = (PullToRefreshListView) findViewById(R.id.collectionListView);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		mListView = pullListView.getRefreshableView();
		registerForContextMenu(mListView);
		adapter = new QAInfoAdapter(this);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Utils.intoQaDetailActivity(MyCollectionActivity.this, (QaResponse)adapter.getItem(position-1));
			}
		});
	}

	private void requestData() {
		manager = new MySendQaManager(this);
		manager.addQaListener(this);
		reqData(currentPage);
	}

	private void reqData(int page) {
		manager.reqData(GlobalContext.accountId, "", TYPE, page * PAGE_COUNTS,PAGE_COUNTS);
	}

	OnRefreshListener onRefreshListener = new OnRefreshListener() {
		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};

	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		++currentPage;
		reqData(currentPage);
	}

	private void intentDetails(QaResponse resp) {
		Utils.intoQaDetailActivity(MyCollectionActivity.this, resp);
	}

	@Override
	public void onQaFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false,false);
	}

	@Override
	public void onQaSuccess(List<QaResponse> list) {
		pullListView.onRefreshComplete(false,false);
		if(null != list){
			if(0 != currentPage){
				adapter.addDataList(list);
			}else{
				adapter.addDataList(list);
			}
			mList = adapter.getList();
			adapter.notifyDataSetChanged();
			if(mList.size() == 0){
				noDataTV.setVisibility(View.VISIBLE);
			}else{
				noDataTV.setVisibility(View.GONE);
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (null != manager) {
			manager.removeQaListener(this);
		}
		QaDataChangeManager.getInstance().removeQaChangeListener(this);
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	public void addOneData(QaResponse addData) {
		// TODO Auto-generated method stub
		noDataTV.setVisibility(View.GONE);
		adapter.addOneQuestData(addData);
		mList = adapter.getList();
	}

	@Override
	public void removeOneData(int questId) {
		// TODO Auto-generated method stub
		adapter.removeOneData(questId);
		mList = adapter.getList();
		if (mList.size() == 0) {
			noDataTV.setVisibility(View.VISIBLE);
		} else {
			noDataTV.setVisibility(View.GONE);
		}
	}

	@Override
	public void priseNotify(int questId, int isGood, int goodCounts) {
		// TODO Auto-generated method stub
		adapter.priseNotify(questId, isGood, goodCounts);
	}

	@Override
	public void replyNotify(int questId, int answerCounts) {
		// TODO Auto-generated method stub
		adapter.replyNotify(questId, answerCounts);
	}
}
